package com.pqc.tbk.dao.user;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.pqc.tbk.dao.BaseDaoSupport;
import com.pqc.tbk.entity.account.User;

@Repository("userDao")
public class UserDaoImpl extends BaseDaoSupport<User> implements UserDao {

	@Override
	public User findByUsername(final String username) {
		final String hql = "from User where username = ? OR email = ? OR tel = ?";
		final List<User> list = em.createQuery(hql, User.class)
				.setParameter(1, username).setParameter(2, username)
				.setParameter(3, username).getResultList();
		if ((list == null) || (list.size() != 1)) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public User findByEmail(final String email) {
		final List<User> list = em
				.createQuery("from User where email = ?", User.class)
				.setParameter(1, email).getResultList();
		if ((list == null) || (list.size() != 1)) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public int countByTel(final String tel) {
		final String hql = "select count(*) from User where tel=?";
		final String count = em.createQuery(hql).setParameter(1, tel)
				.getSingleResult().toString();
		return Integer.parseInt(count);
	}

	@Override
	public int countByAlipay(final String alipay) {
		final String hql = "select count(*) from User where alipay=?";
		final String count = em.createQuery(hql).setParameter(1, alipay)
				.getSingleResult().toString();
		return Integer.parseInt(count);
	}

}
